package engine;

import com.tetris.project.Shr_Res;

import framework.utils.GlobalManager;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Picture;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.PictureDrawable;
import android.util.Log;

public class NextPiece {
	
	private static char[][] nextPieceCollection;
	private static Drawable nextImage;
	private static int mResource;
	private int mWidth, mHeight;
	PictureDrawable mPictureDrawable;
	Picture mPiecePicture;
	Board mBoard;	
	
	public NextPiece(char[][] pieceCollecton, Board board)
	{
		nextPieceCollection = pieceCollecton;
		//selectColor(TetrisPieceManager.getRandomNumber());
		mBoard = board;
		mPiecePicture = new Picture();
		mPictureDrawable =  new PictureDrawable(mPiecePicture);
		constructPiecePicture();
		Log.d(Shr_Res.TAG, "Next Piece Created");
	}
	
	public PictureDrawable getNextPiecePic()
	{
		if(Shr_Res.timeToChangePiece){
			constructPiecePicture();
			Shr_Res.timeToChangePiece = false;
		}
		return mPictureDrawable;
	}
	
	private void selectColor(int color)
	{
		//Select the proper brick to construct the piece depending on the color
		switch(color)
		{
			case 0://Color.BLUE:
				mResource = GlobalManager.mDrawableResources[0];
				break;
			case 1://Color.RED:
				mResource = GlobalManager.mDrawableResources[2];
				break;
			case 2://Color.GREEN:
				mResource = GlobalManager.mDrawableResources[1];
				break;
			case 3://Color.YELLOW:
				mResource = GlobalManager.mDrawableResources[3];
				break;
			case 4://Color.PURPLE:
				mResource = GlobalManager.mDrawableResources[4];
				break;
			default:
				mResource = GlobalManager.mDrawableResources[0];
		}
		nextImage = (GlobalManager.GlobalContext.getResources().getDrawable(mResource));
		mWidth = nextImage.getIntrinsicWidth();
		mHeight = nextImage.getIntrinsicHeight();
	}
	
	private void constructPiecePicture()
	{
		selectColor(TetrisPieceManager.getRandomNumber());
		int dX, dY;
		if (TetrisPieceManager.afterNext != 6) {  //All pieces except |
			Canvas c = mPictureDrawable.getPicture().beginRecording(
					mWidth * TetrisPieceManager.GRID_RESOLUTION,
					mHeight * TetrisPieceManager.GRID_RESOLUTION);
			char currentBlock;
			c.drawColor(Color.TRANSPARENT);
			dX = 0; dY = 0;
			for(int i = 0; i < TetrisPieceManager.NUMBER_BLOCKS_PER_PIECE; i++) {
				currentBlock = (char) (nextPieceCollection[0][i] & TetrisPieceManager.NUMBER_MASK);
				dY =((currentBlock / TetrisPieceManager.GRID_RESOLUTION) * mHeight);
				dX =((currentBlock % TetrisPieceManager.GRID_RESOLUTION) * mWidth);
				nextImage.setBounds(dX, dY, dX + mWidth, dY + mHeight);
				nextImage.draw(c);
			}
			mPictureDrawable.getPicture().endRecording();
		}
		else{
			Canvas c = mPictureDrawable.getPicture().beginRecording(
					mWidth * 1,
					mHeight * 4);
				c.drawColor(Color.TRANSPARENT);
				dX = 0; dY = 0;
				for(int i = 0; i < TetrisPieceManager.NUMBER_BLOCKS_PER_PIECE; i++) {
					nextImage.setBounds(dX, dY, dX + mWidth, dY + mHeight);
					nextImage.draw(c);
					dY += mHeight;
				}
				mPictureDrawable.getPicture().endRecording();
		}
	}
	
	public static void getNewPiece()
	{
		nextPieceCollection = TetrisPieceManager.getRandomPiece();
		//selectColor(TetrisPieceManager.getRandomNumber());
	}
	
	public static char[][] getNextPiece()
	{
		return nextPieceCollection;
	}

	public static Drawable getNextPieceBrick()
	{
		return nextImage;
	}
	
	public static int getNextResource()
	{
		return mResource;
	}
}